******************************************************************************
* Author: Michael Tomz
* Date: December 21, 2001, 4:10 pm
* Language: Stata version 6.0
* Purpose: Run Clarify on British data, save quantities of interest
******************************************************************************

version 6.0

* SET OPTIONS AND LOAD DATA
set mem 100m                     /* allocate memory */
set more off                     /* allow output to scroll */
local year 64                    /* election to be analyzed by this file */
local nsims 500                  /* # of draws of parameters */
local msims 300                  /* # of draws of y to calc 1 expected value */
local doincum 1                  /* estimate incumbency effects? */
capture erase r`year'v.dta       /* erase this file if it exists */
log using runclar`year', replace /* log the results */
use ukout, clear                 /* load the data */
keep if yearst == "`year'"       /* analyze the data one year at a time */
local numobs = _N                /* number of observations in the dataset */

* GENERATE PLACEHOLDERS
gen ev1c = .
gen ev2c = .
gen ev3c = .
label variable ev1c "Expected vote for Cons-C"  /* the "c" stands for "Clarify" */
label variable ev2c "Expected vote for Lab-C"
label variable ev3c "Expected vote for Libs-C"

* CASE 1: FULLY CONTESTED DISTRICTS
sureg (y1 y1lag y2lag coninc labinc libinc) (y2 y1lag y2lag coninc labinc libinc) if fully==1
predict ey1c if fully==1, equation(#1)
predict ey2c if fully==1, equation(#2)
label var ey1c "E(y1) - SUR"
label var ey2c "E(y2) - SUR"
estsimp sureg (y1 y1lag y2lag coninc labinc libinc) (y2 y1lag y2lag coninc labinc libinc) if fully==1, sims(`nsims')
local i 1
while `i' <= `numobs' { 
   di "Year `year', Fully Contested, observation `i' of " `numobs' /* keep track of progress */
   quietly {
      if fully[`i']==1 {                                           /* only for fully contested districts */
         * step 1: expected vote shares
         setx [`i']                                                /* set x's equal to their true values */
         simqi, genev(ev1t ev2t) tfunc(logiti) msims(`msims')      /* expected votes for 1st, 2nd parties */
         gen ev3t= 1 - ev1t - ev2t                                 /* expected vote shares for 3rd party  */
         su ev1t, meanonly                                         /* mean of sampling distrib of E(vote1)*/
         replace ev1c = r(mean) in `i'                             /* save the mean */
         su ev2t, meanonly                                         /* mean of sampling distrib of E(vote2)*/
         replace ev2c = r(mean) in `i'                             /* save the mean */
         su ev3t, meanonly                                         /* mean of sampling distrib of E(vote3)*/
         replace ev3c = r(mean) in `i'                             /* save the mean */
         * step 2: percent correctly predicted                     /* save full sampling distrib of E(vote)*/
         preserve
         keep v1 v2 v3 ev1t ev2t ev3t fully
         replace v1 = v1[`i']                                      /* true vote share for party 1 */
         replace v2 = v2[`i']                                      /* true vote share for party 2 */
         replace v3 = v3[`i']                                      /* true vote share for party 3 */
         replace fully = fully[`i']                                /* this district is fully contested */
         drop if ev1t==. | ev2t==. | ev3t==.
         rename ev1t ev1c
         rename ev2t ev2c
         rename ev3t ev3c
         gen distid = `i'                                          /* district id */
         gen simid = _n                                            /* simulation id 1,2,..nsims         */
         capture confirm file r`year'v.dta                         /* build a large dataset that        */
         if _rc ~= 0 { save r`year'v, replace old }                /*   contains full sampling distrib  */
         else {                                                    /*   of E(vote) for each district    */
            append using r`year'v 
            save r`year'v, replace old                             
         }
         restore
         * step 3: cleanup
         drop ev1t ev2t ev3t                                       /* drop temporary variables */
      }                                                            /* end of if-fully condition */
   }						                   /* end of quietly */
   local i = `i' + 1                                               /* advance to next observation */
}						                   /* end of while loop */
drop `e(allsims)'                                                  /* drops b1 b2 ... */


* CASE 2: PARTIALLY CONTESTED DISTRICTS
qui count if partial==1                                            /* are any partially contested? */
if r(N) > 1 {                                                      /* if yes, analyze that subset */
   estsimp regress ypc y1lag y2lag coninc labinc if partial==1, sims(`nsims')
   local i 1
   while `i' <= `numobs' { 
      di "Year `year', Partially Contested, observation `i' of " `numobs'
      quietly {
         if partial[`i']==1 {
            * step 1: expected vote shares
            setx [`i']
            simqi, genev(ev1t) tfunc(logiti) msims(`msims')         
            gen ev2t = 1 - ev1t
            su ev1t, meanonly
            replace ev1c = r(mean) in `i'
            su ev2t, meanonly
            replace ev2c = r(mean) in `i'
            * step 2: percent correctly predicted
            preserve
            keep v1 v2 v3 ev1t ev2t fully
            replace v1 = v1[`i']
            replace v2 = v2[`i']
            replace v3 = v3[`i']
            replace fully = fully[`i']
            drop if ev1t==. | ev2t==.
            rename ev1t ev1c
            ren ev2t ev2c
            gen ev3c = .
            gen distid = `i'
            gen simid = _n
            append using r`year'v 
            save r`year'v, replace old
            restore
            * step 3: cleanup
            drop ev1t ev2t 
         }                             /* end of if-partial condition */
      }                                /* end of quietly */
      local i = `i' + 1                /* advance to next observation */
   }                                   /* end of while loop */
   drop `e(allsims)'                   /* drops b1 b2 ... */
}                                      /* end of if condition */


* SAVE RESULTS FROM CASE 1 AND CASE 2
local order distid year yearst fully partial v1 v2 v3 y1 y2 ypc /* 
   */ y1lag y2lag coninc labinc libinc ey1 ey2 ey1c ey2c ev1 ev2 /*
   */ ev3 ev1c ev2c ev3c tdf ts11 ts12 ts22
keep `order'
order `order'
compress
save r`year', replace old

* INCUMBENCY EFFECTS (FULLY CONTESTED DISTRICTS)
preserve
estsimp sureg (y1 y1lag y2lag coninc labinc libinc) (y2 y1lag y2lag coninc labinc libinc) if fully==1, sims(`nsims')
setx mean
* expected distribution of votes - open seat
setx coninc 0 labinc 0 libinc 0
simqi, ev genev(open1 open2) tfunc(logiti) msims(`msims')
gen open3 = 1 - open1 - open2
* expected distribution of votes - conservative incumbent
setx coninc 1 labinc 0 libinc 0
simqi, ev genev(con1 con2) tfunc(logiti) msims(`msims')
gen con3 = 1 - con1 - con2
* expected distribution of votes - labour incumbent
setx coninc 0 labinc 1 libinc 0
simqi, ev genev(lab1 lab2) tfunc(logiti) msims(`msims')
gen lab3 = 1 - lab1 - lab2
* expected distribution of votes - liberal incumbent
setx coninc 0 labinc 0 libinc 1
simqi, ev genev(lib1 lib2) tfunc(logiti) msims(`msims')
gen lib3 = 1 - lib1 - lib2
* incumbency effects, in percentage points
gen coneff1 = 100*(con1 - open1)  /* conservative */
gen labeff2 = 100*(lab2 - open2)  /* labour */
gen libeff3 = 100*(lib3 - open3)  /* liberal */
* save the results
keep coneff1 labeff2 libeff3
drop if coneff1==.
gen str3 yearst = "`year'"
compress
save if`year', replace old
restore

log close

